package com.doit.day03

import scala.collection.mutable.ArrayBuffer

object _02_不可变数组 {
  def main(args: Array[String]): Unit = {

    //int [] arr = {1,2,3,4,5}
    val ints: Array[Int] = Array(1, 2, 3, 4, 5,6)

    val ints3: Array[Int] = Array(1,2,2,3)

    //1.将 ints 里面的每一个元素都乘以10 ，返回一个新的数组

    val fun = (a:Int)=>{
      //传出来的可以是任意的类型
      "jingjing  niubi"
    }

    //算子  map
    val strings: Array[String] = ints.map((a:Int)=>{a*10+""})


    //filter

    val ints1: Array[Int] = ints.filter((a: Int) => {
      a % 2 == 0
    })


    val strings1: Array[String] = Array("hello", "hi", "what")

    val flatten: Array[Char] = strings1.flatten
    for (elem <- flatten) {
      println(elem)
    }


    //压平
    val array: Array[Array[Char]] = Array(Array('h', 'e', 'l', 'l', 'o'), Array('h', 'i'), Array('w', 'h', 'a', 't'))

    val res: Array[Char] = array.flatten//Array('h', 'e', 'l', 'l', 'o','h', 'i','w', 'h', 'a', 't')
    for (elem <- res) {
      println(elem)
    }


    val arr1: Array[String] = Array("hello,hi,nihao,haoran,shuaige,liangnv,liangzai", "jingjing,nihao,shen,me,shi,hou,zhao,dao,nan", "peng,you,a")
//    Array("hello","hi","nihao","haoran",".....")

    val stringses: Array[Array[String]] = for (elem <- arr1) yield {
      elem.split(",")
    }

    val array1: Array[Array[String]] = arr1.map((str: String) => {
      //"hello,hi,nihao,haoran,shuaige,liangnv,liangzai"
      val strings2: Array[String] = str.split(",")
      strings2
    })


    val flatten1: Array[String] = stringses.flatten
    println(flatten1)





    //100分

/*    val ints1: Array[Int] = for (elem <- ints) yield {
      elem * 10
    }*/

//方法1
/*    val ints2: Array[Int] = new Array[Int](ints.size)

    for (elem <- 0 until ints.size){
      val i: Int = ints(elem)*10
      ints2.update(elem,i)
    }

    for (elem <- ints2) {
      println(elem)
    }*/

    //可变数组
/*    val ints2: ArrayBuffer[Int] = new ArrayBuffer[Int]()
    ints2.append()*/




    //110
//    val ints2: Array[Int] = ints.map(_ * 10)

//    var fun = (arr:Array[Int])=>{
//      val ints2: ArrayBuffer[Int] = new ArrayBuffer[Int]()
//      for (elem <- arr) {
//        ints2.append(elem*10)
//      }
//      ints2.toArray
//    }





/*    //增删改查
    ints.update(0,100)
    //apply 这个方法很特殊 调用他的时候 可以省略不写
    println(ints(0))

    val distinct: Array[Int] = ints.distinct
    println(distinct.mkString(","))
    println(ints.mkString("_"))*/

    //简单的
/*    val last: Int = ints.last
    println(last)//5  数组中得最后一个元素

    val head: Int = ints.head
    println(head)//1  数组中得第一个元素

    val tail: Array[Int] = ints.tail
    println(tail.mkString(","))//2,2,4,5，6

    println(ints.sum)//20  求个

    val ints1: Array[Int] = ints.take(3)//takeLeft
    println(ints1.mkString(","))//1,2,2

    val ints2: Array[Int] = ints.takeRight(2)
    println(ints2.mkString(","))//5,6*/

    //稀奇古怪的  +:  :+  ++ ++: 都是给原来集合加上元素或者集合  ==》 :取决了元素加的位置
/*    val ints1: Array[Int] = ints.+:(100)
    println(ints1.mkString(","))//100,1,2,2,4,5,6

    val ints2: Array[Int] = ints.:+(100)
    println(ints2.mkString(","))//1,2,2,4,5,6,100

    val ints4: Array[Int] = ints.++(ints3)
    println(ints4.mkString(","))//1,2,2,4,5,6,10,20,30

    val ints5: Array[Int] = ints.++:(ints3)
    println(ints5.mkString(","))//10,20,30,1,2,2,4,5,6*/

    //交集
/*    val ints1: Array[Int] = ints.intersect(ints3)
    println(ints1.mkString(","))//1,2,2

    //差集
    val ints2: Array[Int] = ints.diff(ints3)
    println(ints2.mkString(","))//4,5,6

    //并集
    val ints4: Array[Int] = ints.union(ints3)
    println(ints4.mkString(","))//1,2,2,4,5,6,1,2,2,3*/










  }
}
